package com.lry.crawlers.cartoon.processor;

import com.lry.crawlers.cartoon.entity.CartoonContent;
import com.lry.crawlers.cartoon.entity.CartoonVariable;
import com.lry.crawlers.cartoon.service.CartoonService;
import com.lry.crawlers.fiction.processor.FictionProcessor;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;
import java.util.regex.Pattern;

public class CartoonContentProcessor implements PageProcessor {

    public CartoonVariable cartoonVariable;

    public List<String> list;

    public CartoonContentProcessor(){
    }

    public CartoonContentProcessor(CartoonVariable cartoonVariable, List<String> list){
        this.cartoonVariable = cartoonVariable;
        this.list = list;
    }



    private Site site = Site.me()
            .setSleepTime(1)//采集间隔时间
            .setTimeOut(1000*10)//超时时间
            .setRetrySleepTime(3000)//重试时间
            .setRetryTimes(3);//重试次数

    public Site getSite() {
        return site;
    }
    

    //解析页面
    public void process(Page page) {

        String url = page.getUrl().toString();

            //获取漫画内容信息：content
            String[] contents = cartoonVariable.getContent().split("-!-");
            List<String> content = page.getHtml().xpath(contents[0]).regex(contents.length !=1 ? contents[1] : ".+").all();
            String[] contentPages = cartoonVariable.getContentPage().split("-!-");
            List<String> contentPage = page.getHtml().xpath(contentPages[0]).regex(contentPages.length !=1 ? contentPages[1] : ".+").all();
            if(content.size()==0){
                content.add("章节异常，请联系管理员处理");
            }
            list.add(content.get(0));
            if(contentPage.get(0).contains("http")){
                page.addTargetRequest(contentPage.get(0));
            }else if(contentPage.get(0).contains("/")){
                page.addTargetRequest(cartoonVariable.getProtocol()+"://"+cartoonVariable.getUrl()+contentPage.get(0));
            }else if(!contentPage.get(0).contains("javascript")){
                page.addTargetRequest(url+contentPage.get(0));
            }
            }
        }


